#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <stdbool.h>


#include "common.h"
#include "psql_operation.h"





int print_msg(struct ARG*);



int main()
{
    
    struct  ARG client_arg;
    // 创建套接字
    int serv_socket = socket(AF_INET,SOCK_STREAM,0);
    // 定义服务器sock_addr_in结构体
    struct sockaddr_in sin;
    // 结构体赋值
    sin.sin_family=AF_INET;
    // sin.sin_addr.s_addr=inet_addr("127.0.0.1");
    // INADDR_ANY 整个内网
    sin.sin_addr.s_addr=INADDR_ANY;
    sin.sin_port=htons(11234);
    // 绑定套接字及文件符
    int res=bind(serv_socket,(struct sockaddr*) &sin ,sizeof(sin));
    if(res<0)
    {
        printf("bind error!\n");
        return -1;
    }
    // 监听套接字文件符
    res=listen(serv_socket,5);
    if(res<0)
    {
        printf("listen error!\n");
        return -1;
    }
    // 定义客户端sock_addr_in结构体
    struct sockaddr_in clientaddr;
    int len_client_addr=sizeof(clientaddr);
    // int clientfd;
    while(1)
    {
        int clientfd=accept(serv_socket, (struct sockaddr*) &clientaddr,&len_client_addr);
        // printf("%d",clientaddr.sin_addr.s_addr);
        client_arg.connectfd=clientfd;
        client_arg.client_addr=clientaddr;
   
        pthread_t tid;
        pthread_create(&tid,NULL,print_msg,&client_arg);

    }
    close(res);

}

int print_msg(struct ARG* arg)
{
    char recv_buf[1024];  
    char send_buf[1024];
    bool login_status=false;

    struct USER_PASSWD user_info;
    // 验证登录是否成功 
    do
    {
        
        ssize_t rd=read(arg->connectfd,recv_buf,1024);
        if(rd<0)
        {
            printf(" client is offline !\n");
        }
        
        memset(&user_info,0,sizeof(user_info));
        memcpy(&user_info,recv_buf,sizeof(user_info));
        printf("%s %s",user_info.username,user_info.password);
        login_status=check_user_passwd(&user_info);
        // printf("\nstatus is %d",login_status);
        if(login_status)
        {
            memset(send_buf,0,1024);
            memcpy(send_buf,"#@01",4);
            ssize_t rw=write(arg->connectfd,send_buf,strlen(send_buf)+1);
            printf("%s is logined\n",user_info.username);
        }else
        {
            memset(send_buf,0,1024);
            memcpy(send_buf,"#@02",4);
            ssize_t rw=write(arg->connectfd,send_buf,strlen(send_buf)+1);   
            printf("%s username or password error\n",user_info.username);
        }
    }while(!login_status);
    
    
    int i=0;
    // 登录成功后，接受客户端数据
    while (1)
    {        
        
        int i=0;
        printf("%d ",i++);
        memset(recv_buf,0,1024);
        ssize_t rd=read(arg->connectfd,recv_buf,1024);
        if(rd<0)
        {
            printf(" client is offline !\n");
            
        }
        printf("%d ",rd);
        struct Message_Header message_header;
        memcpy(&message_header,recv_buf,sizeof(struct Message_Header));
        // memcpy(&message_header.n_people,recv_buf+lenght_of_username,sizeof(int));
        printf("%s %d ",message_header.username,message_header.n_people);
        char to_whos[message_header.n_people*lenght_of_username];
        memcpy(to_whos,recv_buf+sizeof(struct Message_Header),message_header.n_people*lenght_of_username);
        // char tmp[sizeof(message_header)];
        // memcpy(tmp,recv_buf,sizeof(message_header));
        // printf("%s %s %c ",tmp,tmp+lenght_of_username,*(tmp+20));
        // printf("%s %d ",message_header.username,message_header.n_people);
        // char to_whos[message_header.n_people][lenght_of_username];
        // printf("%d ",i++);
        // memcpy(to_whos,recv_buf+sizeof(message_header),sizeof(to_whos));
        // printf("%d ",i++);
        // char* data_buf =recv_buf+sizeof(message_header)+sizeof(to_whos);
        // printf("%d ",i++);

        char show_to_whos[message_header.n_people*lenght_of_username];
        char* tmp_show=show_to_whos;
        for(int i=0;i<sizeof(to_whos)/lenght_of_username;i++)
        {
            // printf("%s\n",to_whos+i*lenght_of_username);
            memcpy(tmp_show,to_whos+i*lenght_of_username,strlen(to_whos+i*lenght_of_username));
            tmp_show=tmp_show+strlen(to_whos+i*lenght_of_username);
            memcpy(tmp_show,",",1);
            tmp_show++;
        }
        
        printf("Message from %s to %s:%s\n",user_info.username,show_to_whos,recv_buf+sizeof(struct Message_Header)+message_header.n_people*lenght_of_username);
    }
    close(arg->connectfd);

}